#include "LogUtil.h"

LogUtil* LogUtil::logUtil_ = NULL;

LogUtil::LogUtil() { }

LogUtil::~LogUtil() {
    fout.close();
}

void LogUtil::setOutputFile(std::string filename) {
    if (fout.good()) {
        fout.close();
    }
    std::ofstream fout(filename.c_str());
}

void LogUtil::logWarning(std::string classname, std::string method, 
                         std::string warning) {
    if (fout.good()) {
        std::string output;
        output.append(classname).append(" ").append(method).append(" ")
            .append(warning);
        fout.put((*output.c_str()));
        fout.flush();
    } else {
        std::cout << "WARNING: " << classname << "." << method << " " << warning 
            << std::endl;
    }
}

void LogUtil::logError(std::string classname, std::string method, 
                       std::string error) {
    if (fout.is_open()) {
        std::string output;
        output.append(classname).append(" ").append(method).append(" ")
            .append(error);
        fout.put((*output.c_str()));
        fout.flush();
    } else {
        std::cout << "ERROR: " << classname << "." << method << " " << error 
            << std::endl;
    }
}

void LogUtil::logInfo(std::string classname, std::string method, 
                      std::string info) {
    if (fout.is_open()) {
        std::string output;
        output.append(classname).append(" ").append(method).append(" ")
            .append(info);
        fout.put((*output.c_str()));
        fout.flush();
    } else {
        std::cout << "INFO: " << classname << "." << method << " " << info 
            << std::endl;
    }
}